package com.dmap.knowledge.service.impl;

import com.dmap.knowledge.dao.ArticleColumnAppMapper;
import com.dmap.knowledge.dao.ArticleColumnMapper;
import com.dmap.knowledge.po.ArticleColumn;
import com.dmap.knowledge.po.ArticleColumnApp;
import com.dmap.knowledge.po.ArticleColumnAppExample;
import com.dmap.knowledge.po.ArticleColumnExample;
import com.dmap.knowledge.service.*;
import com.dmap.knowledge.vo.ArticleColumnAppVo;
import com.dmap.knowledge.vo.SelectAppColumn;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zmh
 * @date 18-7-18 下午6:52
 */
@Service
public class ArticleColumnAppExtensionServiceImpl implements ArticleColumnAppExtensionService {
    @Resource
    private ArticleColumnService articleColumnService;
    @Resource
    private ArticleColumnAppMapper articleColumnAppMapper;
    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleColumnMapper articleColumnMapper;
    @Resource
    private ArticleExtensionService articleExtensionService;
    @Resource
    private ArticleColumnExtensionService articleColumnExtensionService;

    @Override
    public int countArticleAppList(String lang, String companyNumber, String appNumber, String key, long order) {
        ArticleColumnAppExample articleColumnAppExample = new ArticleColumnAppExample();
        articleColumnAppExample.setOrderByClause("create_time desc");
        ArticleColumnAppExample.Criteria criteria = articleColumnAppExample.createCriteria();
        criteria.andLangEqualTo(lang);
        if (companyNumber != null) {
            criteria.andCompanyNumberEqualTo(companyNumber);
        }
        if (appNumber != null) {
            criteria.andAppNumberEqualTo(appNumber);
        }
        List<Long> articleColumnListId = new ArrayList<>();
        List<ArticleColumnAppVo> articleColumnAppVoList = new ArrayList<>();
        List<ArticleColumnApp> articleColumnApps = articleColumnAppMapper.selectByExample(articleColumnAppExample);
        if (key != null) {
            ArticleColumnExample articleColumnExample = new ArticleColumnExample();
            ArticleColumnExample.Criteria articleColumnExampleCriteria1 = articleColumnExample.createCriteria();
            articleColumnExampleCriteria1.andKeyNameLike("%" + key + "%");
            articleColumnExample.or().andNameLike("%"+key+"%");
            List<ArticleColumn> articleColumnList = articleColumnMapper.selectByExample(articleColumnExample);
            //取到所有的id
            articleColumnList.forEach(articleColumn -> {
                articleColumnListId.add(articleColumn.getId());
            });
            //如果没有
            if(CollectionUtils.isNotEmpty(articleColumnList)){
                criteria.andArticleColumnIdIn(articleColumnListId);
                articleColumnApps = articleColumnAppMapper.selectByExample(articleColumnAppExample);
                articleColumnApps.forEach(articleColumnApp -> {
                    ArticleColumnAppVo articleColumnAppVo = new ArticleColumnAppVo(articleColumnApp);
                    articleColumnAppVo.setColumnName(articleColumnList.get(0).getName());
                    articleColumnAppVo.setIconUrl(articleColumnList.get(0).getIconUrl());
                    articleColumnAppVo.setKey(articleColumnList.get(0).getKeyName());
                    articleColumnAppVo.setArticleNumber(articleExtensionService.countArticleByColumn(articleColumnApp.getAppNumber(), articleColumnApp.getArticleColumnId()));
                    articleColumnAppVoList.add(articleColumnAppVo);
                });
            }
        } else {
            articleColumnApps.forEach(articleColumnApp -> {
                ArticleColumnAppVo articleColumnAppVo = new ArticleColumnAppVo(articleColumnApp);
                ArticleColumn articleColumn = articleColumnService.selectArticleColumnById(articleColumnApp.getArticleColumnId());
                if (articleColumn != null) {
                    articleColumnAppVo.setColumnName(articleColumn.getName());
                    articleColumnAppVo.setIconUrl(articleColumn.getIconUrl());
                    articleColumnAppVo.setKey(articleColumn.getKeyName());
                    articleColumnAppVo.setArticleNumber(articleExtensionService.countArticleByColumn(articleColumnApp.getAppNumber(), articleColumnApp.getArticleColumnId()));
                    articleColumnAppVoList.add(articleColumnAppVo);

                }
            });
        }
        return articleColumnAppVoList.size();
    }

    @Override
    public SelectAppColumn selectArticleColumnAppList(String lang, String companyNumber, String appNumber, String key, long order, int pageIndex, int pageSize) {
        SelectAppColumn selectAppColumn = new SelectAppColumn();
        ArticleColumnAppExample articleColumnAppExample = new ArticleColumnAppExample();
        articleColumnAppExample.setLimitStart((pageIndex - 1) * pageSize);
        articleColumnAppExample.setLimitEnd(pageSize);
        articleColumnAppExample.setOrderByClause("create_time desc");
        ArticleColumnAppExample.Criteria criteria = articleColumnAppExample.createCriteria();
        criteria.andLangEqualTo(lang);
        if (companyNumber != null) {
            criteria.andCompanyNumberEqualTo(companyNumber);
        }
        if (appNumber != null) {
            criteria.andAppNumberEqualTo(appNumber);
        }
        List<Long> articleColumnListId = new ArrayList<>();
        
        //取到所有的id
        List<ArticleColumnAppVo> articleColumnAppVoList = new ArrayList<>();
        List<ArticleColumnApp> articleColumnApps = articleColumnAppMapper.selectByExample(articleColumnAppExample);
        if (key != null) {
            ArticleColumnExample articleColumnExample = new ArticleColumnExample();
            ArticleColumnExample.Criteria articleColumnExampleCriteria1 = articleColumnExample.createCriteria();
            articleColumnExampleCriteria1.andKeyNameLike("%" + key + "%");
            articleColumnExample.or().andNameLike("%"+key+"%");
            List<ArticleColumn> articleColumnList = articleColumnMapper.selectByExample(articleColumnExample);
            //取到所有的id
            articleColumnList.forEach(articleColumn -> {
                articleColumnListId.add(articleColumn.getId());
            });
            //如果没有
            if(CollectionUtils.isNotEmpty(articleColumnList)){
                criteria.andArticleColumnIdIn(articleColumnListId);
                articleColumnApps = articleColumnAppMapper.selectByExample(articleColumnAppExample);
                articleColumnApps.forEach(articleColumnApp -> {
                    ArticleColumnAppVo articleColumnAppVo = new ArticleColumnAppVo(articleColumnApp);
                    articleColumnAppVo.setColumnName(articleColumnList.get(0).getName());
                    articleColumnAppVo.setIconUrl(articleColumnList.get(0).getIconUrl());
                    articleColumnAppVo.setKey(articleColumnList.get(0).getKeyName());
                    articleColumnAppVo.setArticleNumber(articleExtensionService.countArticleByColumn(articleColumnApp.getAppNumber(), articleColumnApp.getArticleColumnId()));
                    articleColumnAppVoList.add(articleColumnAppVo);
                });
            }
        } else {
            articleColumnApps.forEach(articleColumnApp -> {
                ArticleColumnAppVo articleColumnAppVo = new ArticleColumnAppVo(articleColumnApp);
                ArticleColumn articleColumn = articleColumnService.selectArticleColumnById(articleColumnApp.getArticleColumnId());
                if (articleColumn != null) {
                    articleColumnAppVo.setColumnName(articleColumn.getName());
                    articleColumnAppVo.setIconUrl(articleColumn.getIconUrl());
                    articleColumnAppVo.setKey(articleColumn.getKeyName());
                    articleColumnAppVo.setArticleNumber(articleExtensionService.countArticleByColumn(articleColumnApp.getAppNumber(), articleColumnApp.getArticleColumnId()));
                    articleColumnAppVoList.add(articleColumnAppVo);
                }
            });
        }
        selectAppColumn.setCount(countArticleAppList(lang, companyNumber, appNumber, key, order));
        selectAppColumn.setColumnAppList(articleColumnAppVoList);
        return selectAppColumn;
    }
    
    public static void main(String[] args) {
        ArticleColumnAppExtensionServiceImpl articleColumnExtensionService = new ArticleColumnAppExtensionServiceImpl();
        articleColumnExtensionService.selectArticleColumnAppList("zh_cn",null,null,"生活",1,1,10);
    }
    @Override
    public List<ArticleColumnApp> selectArticleColumnAppDownList(String lang, String appNumber) {
        List<ArticleColumnApp> articleColumnAppList;
        ArticleColumnAppExample articleColumnAppExample = new ArticleColumnAppExample();
        ArticleColumnAppExample.Criteria criteria = articleColumnAppExample.createCriteria();
        if (lang != null) {
            criteria.andLangEqualTo(lang);

        }
        if (appNumber != null) {
            criteria.andAppNumberEqualTo(appNumber);
        }
        //必须启用才能查找
        criteria.andStatusEqualTo(1);
        articleColumnAppList = articleColumnAppMapper.selectByExample(articleColumnAppExample);
        return articleColumnAppList;
    }

    @Override
    public int countArticleColumnAppByColumnId(long columnId) {
        ArticleColumnAppExample articleColumnAppExample = new ArticleColumnAppExample();
        ArticleColumnAppExample.Criteria criteria = articleColumnAppExample.createCriteria();
        criteria.andArticleColumnIdEqualTo(columnId);
        return articleColumnAppMapper.countByExample(articleColumnAppExample);
    }

    @Override
    public int checkColumnAppList(String lang, String appNumber, String columnId, String appColumnId) {
        int count;
        ArticleColumnAppExample articleColumnAppExample = new ArticleColumnAppExample();
        ArticleColumnAppExample.Criteria criteria = articleColumnAppExample.createCriteria();
        if (lang != null) {
            criteria.andLangEqualTo(lang);
        }
        if (appNumber != null) {
            criteria.andAppNumberEqualTo(appNumber);
        }
        if (columnId != null) {
            criteria.andArticleColumnIdEqualTo(Long.parseLong(columnId));
        }
        if (appColumnId != null) {
            criteria.andIdNotEqualTo(Long.parseLong(appColumnId));
        }
        count = articleColumnAppMapper.countByExample(articleColumnAppExample);
        return count;
    }
}
